<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FormBasedPage
 *
 * @author julien
 */
require_once 'Classes/View/DefaultHTMLPage.php';
require_once 'Classes/View/ViewItems/FormItem.php';

class FormBasedPage extends DefaultHTMLPage {
    
    private $form;
    public function __construct($title) {
        parent::__construct($title);
        $this->form = new FormItem();
        $this->form->enableLabels();
        $this->addContent($this->form);
    }
    
   
    public function addElement($element){
        if(is_array($element)){
            foreach ($element as $key => $value) {
                $this->addElement($value);
            }
        }
        else if(is_a($element,"Element")){
            
            if(method_exists($element, "getName")){
                $this->form->addInputField("Title", "text", $element->getName());
            }
            
            if(method_exists($element, "getDescription")){
                 $this->form->addInputField("Description", "text", $element->getName());
            }
            
            //Je commence ici

            if(method_exists($element, "getBarcode")){
                 $this->form->addInputField("BarCode", "text", $element->getBarcode());
            }
            
            if(method_exists($element, "getStatut")){
                 $this->form->addSelector("Status", array('OK','NOK','AREPARER'), 'OK');

            }
            
            if(method_exists($element, "getLoanID")){
                require_once 'Classes/Model/LoanModel.php';
                $loanArray = LoanModel::getInstance()->getAllLoans();
                $loanNameArray = array();
                $loanIDArray = array();

                
                foreach ($loanArray as $key => $value) {
                    array_push($loanNameArray, $value->getID());
                    array_push($loanIDArray, $value->getID());
                }
                array_push($loanNameArray, "null");
                    array_push($loanIDArray, null);
                    
                $this->form->addSelector("Loans", $loanNameArray, null, $loanIDArray);
            }

              
             if(method_exists($element, "getSubfamilyID")){
             require_once 'Classes/Model/Sub_FamilyModel.php';
                $familyArray = Sub_FamilyModel::getInstance()->listAllSub_Families();
                $familyNameArray = array();
                $familyIDArray = array();

                
                foreach ($familyArray as $key => $value) {
                    array_push($familyNameArray, $value->getName());
                    array_push($familyIDArray, $value->getID());
                }
                $this->form->addSelector("Subfamily", $familyNameArray, HTTPRequestModel::getInstance()->extractGETValue('subfamily'),$familyIDArray);
            }    
            //////////////// Ici: mettre un selector pour les subfamily
              else if(method_exists($element, "getFamilyID")){
                require_once 'Classes/Model/FamilyModel.php';
                $familyArray = FamilyModel::getInstance()->listFamily();
                $familyNameArray = array();
                $familyIDArray = array();

                foreach ($familyArray as $key => $value) {
                    array_push($familyNameArray, $value->getName());
                    array_push($familyIDArray, $value->getID());

                }
                $this->form->addSelector("Family", $familyNameArray, HTTPRequestModel::getInstance()->extractGETValue('family'),$familyIDArray);
            }
          
            
            
            //////////////// Ici: mettre un selector pour les structures
            if(method_exists($element, "getStructureID")){
                require_once 'Classes/Model/FamilyModel.php';

                require_once 'Classes/Model/StructureModel.php';
                $familyArray = StructureModel::getInstance()->getAllStructures();
                $familyNameArray = array();
                $familyIDArray = array();
                
                foreach ($familyArray as $key => $value) {
                    array_push($familyNameArray, $value->getName());
                    array_push($familyIDArray, $value->getID());
                }
                $this->form->addSelector("Structure", $familyNameArray, HTTPRequestModel::getInstance()->extractGETValue('family'), $familyIDArray);
            }
            
          
            
            //Je termine là
            if(method_exists($element, "getOptionalFields")){
                    $this->form->addInputField("Opt1", "text");
                    $this->form->addInputField("Opt2", "text");
                    $this->form->addInputField("Opt3", "text");
                    $this->form->addInputField("Opt4", "text");
                    $this->form->addInputField("Opt5", "text");
                    $this->form->addInputField("Opt6", "text");
                    
               
            }
            
            
            if(method_exists($element,"getAddress")){
                $this->form->addInputField("Address", "text",$element->getAddress());
            }
             
            
               
        }
    }
}

?>
